﻿$(document).ready(function () {

//    applyDateCalender("txtDob");
//    applyDateCalender("txtJoiningDate");
//    applyDateCalender("txtLeavingDate");
//    applyDateCalender("txtPassingYear_1");
    GetMaxEmpId();
});

function GetEmployeesList() {
    $.ajax({
        type: "POST",
        url: "Employees-list.aspx/GetEmployeesList",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: GetEmployeesListSuccess,
        error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Error occurred in getting Employees Details"); }
    });

}
function GetEmployeesListSuccess(response) {
    debugger;
    var employees = response.d;
    return employees;
}
function SaveEmployeeInfo() {

//    $("#divmodelPop").jqm({ overlay: 50, modal: true, trigger: false });
//    $("#divmodelPop").jqmShow();

    if ($.trim($("#txtEmpName").val()) == "") {
        showErrorDiv("txtEmpName", "Please enter Name");
        return false;
    }
    var EmpObj = {};
    EmpObj["Emp_Id"] = $.trim($("#txtEmpNo").val());
    EmpObj["Emp_Name"] = $.trim($("#txtEmpName").val());
    EmpObj["Father_Name"] = $.trim($("#txtFatherName").val());
    EmpObj["Gender"] = $("#ddlGender").val();
    EmpObj["Date_of_Birth"] = $.trim($("#txtDob").val());
    EmpObj["Joining_Date"] = $.trim($("#txtJoiningDate").val());
    EmpObj["Blood_Group"] = $.trim($("#ddlBloodGrp").val());
    EmpObj["NIC"] = $.trim($("#txtNic").val());
    EmpObj["Marital_Status"] = $("#ddlMaritalStatus").val();
    EmpObj["Designation"] = $("#ddlDesg").val();
    EmpObj["Leaving_Date"] = $.trim($("#txtLeavingDate").val());
    EmpObj["Salary"] = $.trim($("#txtSalary").val());

    //********************  Contact Info****************
    EmpObj["Home_Address"] = $.trim($("#txtHomeAdd").val());
    EmpObj["Home_Phone"] = $.trim($("#txtPhoneHome").val());
    EmpObj["Cell_No"] = $.trim($("#txtCellNo").val());
    EmpObj["Email_Address"] = $.trim($("#txtEmail").val());

    var AcedamicInfoArry = new Array();
    $("#tblAcedamicInfo  tr:gt(0)").each(function myfunction() {
        var obj = {};

        var Qualification = $(this).find("td:eq(0) select").val();
        var PassingYear = $.trim($(this).find("td:eq(1) input").val());
        var Division = $.trim($(this).find("td:eq(2) input").val());
        var Institute = $.trim($(this).find("td:eq(3) input").val());

        if (Qualification == "" && PassingYear == "" && Division == "" && Institute == "") {

        }
        else {
            obj["Degree_Id"] = Qualification;
            obj["Passing_Year"] = PassingYear;
            obj["Division"] = Division;
            obj["Institute"] = Institute;
            obj["Emp_Id"] = $.trim($("#txtEmpNo").val()); 
            AcedamicInfoArry.push(obj);
        }
    });

    var request = "{'EmpInfo':" + JSON.stringify(EmpObj) + ",'EmpQualification':" + JSON.stringify(AcedamicInfoArry) + "}";
    $.ajax({
        type: "POST",
        url: "Employees-list.aspx/SaveEmployeeInfo",
        data: request,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: SaveEmployeeInfoSuccess,
        error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Error occurred in Saveing Employee Information"); }
    });
    
}
function SaveEmployeeInfoSuccess(response) {
    alert("Information Saved  Sucessfully");
}
function GetMaxEmpId() {
    var request = "{}";
    $.ajax({
        type: "POST",
        url: "Employees-list.aspx/GetMaxEmpId",
        data: request,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: GetMaxEmpIdSuccess,
        error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Error occurred in Getting Employee Id"); }
    });
    function GetMaxEmpIdSuccess(response) {
        $("#txtEmpNo").val(response.d);
    }
}